package com.burpee.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.burpee.service.AddfileService;
import com.burpee.service.ExerciseScheduleService;
import com.burpee.service.ExerciseService;
import com.burpee.service.FoodScheduleService;
import com.burpee.service.FoodService;
import com.burpee.ui.Addfile;
import com.burpee.ui.Exercise;
import com.burpee.ui.ExerciseSchedule;
import com.burpee.ui.Food;
import com.burpee.ui.FoodSchedule;
import com.burpee.ui.Member;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


@Controller
@RequestMapping("/schedule.spring")
public class ScheduleController {

	@Autowired ExerciseService exerciseService;
	@Autowired FoodService foodService;
	@Autowired ExerciseScheduleService exerciseScheduleService;
	@Autowired FoodScheduleService foodScheduleService;
	@Autowired AddfileService addfileService;


	//	@Autowired
	//	ExerciseOrderService exerciseOrderService;
	//	@Autowired
	//	ChoiceService choiceService;



	/**
	 * @Method Name : foodMain
	 * @date : 2013. 8. 1.
	 * @author : LEESANGUCK
	 * @history : 음식/영양 
	 * @param request * @param response * @param food
	 * @return
	 */ 	
	@RequestMapping(params ="action=foodMain")
	public ModelAndView foodMain( HttpServletRequest request,
			HttpServletResponse response,
			String foodIngestPurpose, String foodClass, String foodClassfication){

		HashMap<String, String> foodchoice = new HashMap<String, String>();
		if(foodIngestPurpose == null || foodIngestPurpose.equals("")) {
			foodIngestPurpose = null;
		}
		if(foodClass == null || foodClass.equals("")) {
			foodClass = null;
		}
		if(foodClassfication == null || foodClassfication.equals("")) {
			foodClassfication = null;
		}
		foodchoice.put("foodIngestPurpose", foodIngestPurpose);
		foodchoice.put("foodClass", foodClass);
		foodchoice.put("foodClassfication", foodClassfication);

		
		List<Food> foodList = foodService.list(foodchoice);
		List<Food> foodClassList =foodService.foodClassList();
		List<Food> foodClassficationList =foodService.foodClassficationList();

		ModelAndView modelAndView = new ModelAndView();

		modelAndView.addObject("foodchoice",foodchoice);
		modelAndView.addObject("foodList",foodList);
		modelAndView.addObject("foodClassList",foodClassList);
		modelAndView.addObject("foodClassficationList",foodClassficationList);
		modelAndView.setViewName("food/foodMain");
		return modelAndView;
	}

	@RequestMapping(params ="action=informationView")
	public ModelAndView informationView (HttpServletRequest request,
			HttpServletResponse response, long foodNo) {
		
		List<Addfile> addfiles = addfileService.listAddFileByFoodNo(foodNo);
		
		ModelAndView modelAndView = new ModelAndView();

		Food food = foodService.view(foodNo);
		modelAndView.addObject("foodfile", addfiles);
		modelAndView.addObject("food",food);
		modelAndView.setViewName("food/foodInformationView");
		return modelAndView;
	}

	@RequestMapping(params ="action=totalFoodInformation")
	public ModelAndView totalFoodInformation( HttpServletRequest request,
			HttpServletResponse response,
			Food food ){

		List<Food>list = null;


		ModelAndView modelAndView = new ModelAndView();

		modelAndView.addObject("foodList", list);
		modelAndView.addObject("food", "A");
		modelAndView.setViewName("food/foodInformationPage");
		return modelAndView;
	}

	@RequestMapping(params ="action=foodClassSelectList")
	public ModelAndView foodClassSelectList( HttpServletRequest request,
			HttpServletResponse response,
			Food food){

		return null;
	}

	@RequestMapping(params ="action=foodIngestPurposeSelectList")
	public ModelAndView foodIngestPurposeSelectList( HttpServletRequest request,
			HttpServletResponse response,
			Food food){
		List<Food>list = null;
		food.setFoodIngestPurpose(request.getParameter("foodIngestPurpose"));

		list = foodService.foodIngestPurposeSelectList(food);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("foodList", list);
		modelAndView.addObject("food", food.getFoodIngestPurpose());
		modelAndView.setViewName("food/foodInformationPage");
		return modelAndView;
	}




	/**
	 * @Method Name : exerciselist
	 * @date : 2013. 8. 2.
	 * @author : LEESANGUCK
	 * @history :  헬스 관리
	 * @param request * @param response * @param exercise
	 * @return
	 */

	@RequestMapping(params = "action=exerciseMain")
	public ModelAndView exerciseMain(HttpServletRequest request, HttpServletResponse response){

		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String exercisePurpose =request.getParameter("exercisePurpose");
		String exerciseRegio=request.getParameter("exerciseRegio");

		HashMap<String, String> exerciseChoice = new HashMap<String, String>();
		if(exercisePurpose == null || exercisePurpose.equals("")) {
			exercisePurpose = null;
		}
		if(exerciseRegio == null || exerciseRegio.equals("")) {
			exerciseRegio = null;
		} else
		{
			try {
				exerciseRegio = new String(exerciseRegio.getBytes("8859_1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		exerciseChoice.put("exercisePurpose", exercisePurpose);
		exerciseChoice.put("exerciseRegio", exerciseRegio);

		Exercise exercise = new Exercise();
		exercise.setExercisePurpose(exercisePurpose);
		exercise.setExerciseRegio(exerciseRegio);

		List<Exercise> list = exerciseService.list(exercise);

		ModelAndView modelAndView = new ModelAndView();

		modelAndView.addObject("exerciseList",list);
		modelAndView.addObject("exerciseChoice",exerciseChoice);
		modelAndView.setViewName("health/healthMain");

		return modelAndView;


	}

	@RequestMapping(params = "action=exerciselist")
	public ModelAndView exerciselist(HttpServletRequest request, HttpServletResponse response, Exercise exercise){ List<Exercise> exerciselist = exerciseService.list(exercise);
	ModelAndView modelAndView = new ModelAndView();
	modelAndView.addObject("exerciseList",exerciselist);
	modelAndView.setViewName("health/mysports");

	return modelAndView;

	}

	@RequestMapping(params ="action=exercisePurposeSelectList")
	public ModelAndView exercisePurposeSelectList( HttpServletRequest request, 
			HttpServletResponse response,
			Exercise exercise){
		List<Exercise>list = null;

		exercise.setExercisePurpose(request.getParameter("exercisePurpose"));
		list = exerciseService.exercisePurposeSelectList(exercise);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("exerciseList", list);
		modelAndView.addObject("exercise", exercise.getExercisePurpose());
		modelAndView.setViewName("health/sportInformationPage");

		return modelAndView;
	}

	@RequestMapping(params ="action=myFood")
	public ModelAndView myFood( HttpServletRequest request,
			HttpServletResponse response,String foodIngestPurpose, String foodClass, String foodClassfication, String foodScheduleStartdate){
		ModelAndView modelAndView = new ModelAndView();

		Member member = (Member)request.getSession().getAttribute("login");

		if(foodScheduleStartdate == null) {
			SimpleDateFormat mdate = new SimpleDateFormat("yyyy-MM-dd",Locale.KOREA);			
			Date date = new Date(System.currentTimeMillis());
			foodScheduleStartdate = mdate.format(date);
		}

		List<Food> foodClassList =foodService.foodClassList();
		List<Food> foodClassficationList =foodService.foodClassficationList();

		HashMap<String, String> foodchoice = new HashMap<String, String>();

		if(foodIngestPurpose == null || foodIngestPurpose.equals("")) {
			foodIngestPurpose = null;
		}
		if(foodClass == null || foodClass.equals("")) {
			foodClass = null;
		}
		if(foodClassfication == null || foodClassfication.equals("")) {
			foodClassfication = null;
		}

		foodchoice.put("foodIngestPurpose", foodIngestPurpose);
		foodchoice.put("foodClass", foodClass);
		foodchoice.put("foodClassfication", foodClassfication);

		List<Food> list = foodService.list(foodchoice);

		FoodSchedule foodSchedule = new FoodSchedule();
		foodSchedule.setFoodScheduleStartdate(Date.valueOf(foodScheduleStartdate));
		foodSchedule.setMemberId(member.getMemberId());
		modelAndView.addObject("foodSchedule",foodSchedule);

		foodSchedule.setFoodScheduleChoice("B");	
		FoodSchedule foodScheduleB = foodScheduleService.view(foodSchedule);
		modelAndView.addObject("foodScheduleB",foodScheduleB);

		foodSchedule.setFoodScheduleChoice("L");
		FoodSchedule foodScheduleL = foodScheduleService.view(foodSchedule);
		modelAndView.addObject("foodScheduleL",foodScheduleL);

		foodSchedule.setFoodScheduleChoice("D");
		FoodSchedule foodScheduleD = foodScheduleService.view(foodSchedule);
		modelAndView.addObject("foodScheduleD",foodScheduleD);

		modelAndView.addObject("foodlist",list);
		modelAndView.addObject("foodchoice",foodchoice);
		modelAndView.addObject("foodClassList",foodClassList);
		modelAndView.addObject("foodClassficationList",foodClassficationList);
		modelAndView.setViewName("food/myFood");
		return modelAndView;
	}

	@RequestMapping(params ="action=myFoodSchedule")
	public ModelAndView myFoodSchedule( HttpServletRequest request,
			HttpServletResponse response, String foodClass, String foodClassfication){
		ModelAndView modelAndView = new ModelAndView();
		List<Food> foodClassList =foodService.foodClassList();
		List<Food> foodClassficationList =foodService.foodClassficationList();
		HashMap<String, String> foodchoice = new HashMap<String, String>();

		if(foodClass == null || foodClass.equals("")) {
			foodClass = null;
		}
		if(foodClassfication == null || foodClassfication.equals("")) {
			foodClassfication = null;
		}

		foodchoice.put("foodIngestPurpose", null);
		foodchoice.put("foodClass", foodClass);
		foodchoice.put("foodClassfication", foodClassfication);

		List<Food> list = foodService.list(foodchoice);

		modelAndView.addObject("foodlist",list);
		modelAndView.addObject("foodchoice",foodchoice);
		modelAndView.addObject("foodClassList",foodClassList);
		modelAndView.addObject("foodClassficationList",foodClassficationList);
		modelAndView.setViewName("food/myFoodView");

		return modelAndView;
	}

	@RequestMapping(params = "action=myFoodAdd")
	public ModelAndView myFoodAdd(HttpServletRequest request, HttpServletResponse response){ 
		ModelAndView modelAndView = new ModelAndView();
		Member member = (Member)request.getSession().getAttribute("login");
		String button = request.getParameter("foodScheduleChoice");
		String date = request.getParameter("foodScheduleStartdate");
		String foodNo = request.getParameter("foodNo");

		FoodSchedule foodSchedule = new FoodSchedule();
		foodSchedule.setMemberId(member.getMemberId());
		foodSchedule.setFoodNo(Long.parseLong(foodNo));
		foodSchedule.setFoodScheduleChoice(button);
		foodSchedule.setFoodScheduleStartdate(Date.valueOf(date));

		FoodSchedule check = foodScheduleService.view(foodSchedule);
		if( check == null) {			
			foodScheduleService.add(foodSchedule);
		} else
		{
			foodSchedule.setFoodScheduleNo(check.getFoodScheduleNo());
			foodScheduleService.modify(foodSchedule);
		}


		return myFood(request, response, null, null, null, date);

	}

	@RequestMapping(params = "action=myFoodRemove")
	public ModelAndView myFoodRemove(HttpServletRequest request, HttpServletResponse response,String foodScheduleNo, String foodScheduleStartdate){ 
		ModelAndView modelAndView = new ModelAndView();
		FoodSchedule foodSchedule = new FoodSchedule();
		foodSchedule.setFoodScheduleNo(Long.parseLong(foodScheduleNo));

		foodScheduleService.remove(foodSchedule);

		return myFood(request, response, null, null, null, foodScheduleStartdate);

	}

	@RequestMapping(params = "action=mySports")
	public ModelAndView mySports(HttpServletRequest request, HttpServletResponse response, String exercisePurpose, String exerciseRegio, String exerciseScheduleStartdate){ 

		ModelAndView modelAndView = new ModelAndView();
		Member member = (Member)request.getSession().getAttribute("login");

		if(exerciseScheduleStartdate == null) {
			SimpleDateFormat mdate = new SimpleDateFormat("yyyy-MM-dd",Locale.KOREA);			
			Date date = new Date(System.currentTimeMillis());
			exerciseScheduleStartdate = mdate.format(date);
		}

		if(exercisePurpose == null || exercisePurpose.equals("")) {
			exercisePurpose = null;
		}
		if(exerciseRegio == null || exerciseRegio.equals("")) {
			exerciseRegio = null;
		}
		Exercise exercise = new Exercise();
		exercise.setExercisePurpose(exercisePurpose);
		exercise.setExerciseRegio(exerciseRegio);

		List<Exercise> list = exerciseService.list(exercise);
		List<Exercise> regiolist = exerciseService.exerciseRegioList();

		ExerciseSchedule exerciseSchedule = new ExerciseSchedule();
		exerciseSchedule.setMemberId(member.getMemberId());
		exerciseSchedule.setExerciseScheduleStartdate(Date.valueOf(exerciseScheduleStartdate));

		exerciseSchedule.setExerciseScheduleClass("W");
		List<ExerciseSchedule> exScheduleW = exerciseScheduleService.schedulelist(exerciseSchedule);
		modelAndView.addObject("exScheduleW",exScheduleW);

		exerciseSchedule.setExerciseScheduleClass("U");
		List<ExerciseSchedule> exScheduleU = exerciseScheduleService.schedulelist(exerciseSchedule);
		modelAndView.addObject("exScheduleU",exScheduleU);

		exerciseSchedule.setExerciseScheduleClass("M");
		List<ExerciseSchedule> exScheduleM = exerciseScheduleService.schedulelist(exerciseSchedule);
		modelAndView.addObject("exScheduleM",exScheduleM);

		exerciseSchedule.setExerciseScheduleClass("L");
		List<ExerciseSchedule> exScheduleL = exerciseScheduleService.schedulelist(exerciseSchedule);
		modelAndView.addObject("exScheduleL",exScheduleL);

		exerciseSchedule.setExerciseScheduleClass("A");
		List<ExerciseSchedule> exScheduleA = exerciseScheduleService.schedulelist(exerciseSchedule);
		modelAndView.addObject("exScheduleA",exScheduleA);

		modelAndView.addObject("exerciseScheduleStartdate",exerciseScheduleStartdate);
		modelAndView.addObject("exerciseList",list);
		modelAndView.addObject("regioList",regiolist);
		modelAndView.setViewName("health/mySports");
		return modelAndView;

	}
	@RequestMapping(params = "action=mySportsView")
	public ModelAndView mySportsView(HttpServletRequest request, HttpServletResponse response, long exerciseNo){ 

		ModelAndView modelAndView = new ModelAndView();

		Exercise exercise = exerciseService.view(exerciseNo);

		modelAndView.addObject("exerciseView",exercise);
		modelAndView.setViewName("health/mySportsView");
		return modelAndView;

	}

	@RequestMapping(params = "action=myExerciseAdd")
	public ModelAndView myExerciseAdd(HttpServletRequest request, HttpServletResponse response,
			String exerciseNo, String exerciseScheduleStartdate, String exerciseScheduleCount,
			String exerciseScheduleRuntime, String exerciseScheduleClass){
		Member member = (Member)request.getSession().getAttribute("login");
		ModelAndView modelAndView = new ModelAndView();
		ExerciseSchedule exerciseSchedule = new ExerciseSchedule();

		Exercise exercise = exerciseService.view(Long.parseLong(exerciseNo));

		exerciseSchedule.setMemberId(member.getMemberId());
		exerciseSchedule.setExerciseNo(Long.parseLong(exerciseNo));
		exerciseSchedule.setExerciseScheduleRuntime(Long.parseLong(exerciseScheduleRuntime));
		exerciseSchedule.setExerciseScheduleCount(Long.parseLong(exerciseScheduleCount));
		exerciseSchedule.setExerciseScheduleStartdate(Date.valueOf(exerciseScheduleStartdate));
		exerciseSchedule.setExerciseScheduleStarttime(Date.valueOf(exerciseScheduleStartdate));
		exerciseSchedule.setExerciseScheduleAchieveYn("N");
		exerciseSchedule.setExerciseScheduleSetnum(exercise.getExerciseAverageSetnum());
		exerciseSchedule.setExerciseScheduleClass(exerciseScheduleClass);

		exerciseScheduleService.add(exerciseSchedule);			

		return mySports(request, response, null, null, exerciseScheduleStartdate);

	}

	@RequestMapping(params = "action=scheduleCalendar")
	public void scheduleCalendar(HttpServletRequest request, HttpServletResponse response){ 

		//		title : 'Click for Google',
		//		start : new Date(y, m, 28),
		//		end : new Date(y, m, 29),
		//		url : 'http://google.com/'
		Member member = (Member)request.getSession().getAttribute("login");
		FoodSchedule foodSchedule = new FoodSchedule();
		foodSchedule.setMemberId(member.getMemberId());
		List<FoodSchedule> foodList = foodScheduleService.list(foodSchedule);

		ObjectMapper mapper = new ObjectMapper();

		response.setContentType("text/html; charset=UTF-8");
		try {
			mapper.writeValue(response.getWriter(), foodList);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping(params = "action=CalendarPopup")
	public ModelAndView CalendarPopup(HttpServletRequest request, HttpServletResponse response, String ScheduleStartdate){
		Member member = (Member)request.getSession().getAttribute("login");
		ModelAndView modelAndView = new ModelAndView();
		ExerciseSchedule exerciseSchedule = new ExerciseSchedule();

		exerciseSchedule.setMemberId(member.getMemberId());
		exerciseSchedule.setExerciseScheduleStartdate(Date.valueOf(ScheduleStartdate));

		exerciseSchedule.setExerciseScheduleClass("W");
		List<ExerciseSchedule> exScheduleW = exerciseScheduleService.schedulelist(exerciseSchedule);
		modelAndView.addObject("exScheduleW",exScheduleW);

		exerciseSchedule.setExerciseScheduleClass("U");
		List<ExerciseSchedule> exScheduleU = exerciseScheduleService.schedulelist(exerciseSchedule);
		modelAndView.addObject("exScheduleU",exScheduleU);

		exerciseSchedule.setExerciseScheduleClass("M");
		List<ExerciseSchedule> exScheduleM = exerciseScheduleService.schedulelist(exerciseSchedule);
		modelAndView.addObject("exScheduleM",exScheduleM);

		exerciseSchedule.setExerciseScheduleClass("L");
		List<ExerciseSchedule> exScheduleL = exerciseScheduleService.schedulelist(exerciseSchedule);
		modelAndView.addObject("exScheduleL",exScheduleL);

		exerciseSchedule.setExerciseScheduleClass("A");
		List<ExerciseSchedule> exScheduleA = exerciseScheduleService.schedulelist(exerciseSchedule);
		modelAndView.addObject("exScheduleA",exScheduleA);
		
		FoodSchedule foodSchedule = new FoodSchedule();
		foodSchedule.setFoodScheduleStartdate(Date.valueOf(ScheduleStartdate));
		foodSchedule.setMemberId(member.getMemberId());
		modelAndView.addObject("foodSchedule",foodSchedule);

		foodSchedule.setFoodScheduleChoice("B");	
		FoodSchedule foodScheduleB = foodScheduleService.view(foodSchedule);
		modelAndView.addObject("foodScheduleB",foodScheduleB);

		foodSchedule.setFoodScheduleChoice("L");
		FoodSchedule foodScheduleL = foodScheduleService.view(foodSchedule);
		modelAndView.addObject("foodScheduleL",foodScheduleL);

		foodSchedule.setFoodScheduleChoice("D");
		FoodSchedule foodScheduleD = foodScheduleService.view(foodSchedule);
		modelAndView.addObject("foodScheduleD",foodScheduleD);
		
		modelAndView.setViewName("schedule/schedulepopup");
		
		return modelAndView;

	}


	public StringBuffer scheduleControl() {
		StringBuffer result = null;




		return result;		
	}

}






